package L4;

import java.util.Scanner;

class Point2D {
    protected double x, y;
    public Point2D(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public Point2D() {
        this(0, 0);
    }
    //返回坐标形式的字符串
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
    //判断两个二维点是否重合
    public boolean equals(Point2D p) {
        return x == p.x && y == p.y;
    }
    //求到原点距离
    public double getDistance() {
        return Math.sqrt(x * x + y * y);
    }
    //判断是否是原点
    public boolean isZero() {
        return getDistance() == 0;
    }
}

class Point3D extends Point2D {
    private double z;
    Point3D(double x,double y,double z) {
        super(x, y);
        this.z = z;
    }
    Point3D(){
        this(0,0,0);
    }
    @Override
    public String toString() {
        return String.format("(%.1f,%.1f,%.1f)", x,y,z);
    }
    @Override
    public boolean isZero() {
        return getDistance() == 0;
    }
    @Override
    public double getDistance() {
        return Math.sqrt(x*x+y*y+z*z);
    }
    public boolean equals(Point3D p) {
        return x == p.x && y == p.y && z == p.z;
    }
}

public class TestPoint {
    public static String filter(double d){
        String s = String.format("%.15f", d);
        int ind;
        for (ind = s.length()-1;ind>=0; --ind) {
            if (s.charAt(ind) != '0')
                break;
        }
        if (s.charAt(ind) == '.')
            ind += 2;
        else 
            ++ind;
        return s.substring(0,ind);
    }
    public static void main(String[] args) {
        double x;
        double y;
        double z;
        Point2D point2d1;
        Point2D point2dOrg = new Point2D();

        System.out.println("Please enter the coordinates of p21:");
        Scanner sc = new Scanner(System.in);
        x = sc.nextDouble();
        y = sc.nextDouble();
        point2d1 = new Point2D(x,y);
        System.out.printf("The distance from %s to the origin is %s%n",point2d1,filter(point2d1.getDistance()));
        System.out.printf("Is %s the origin? -- %s%n",point2d1,point2d1.isZero());
        System.out.printf("Is (0,0) the origin? -- %s%n",point2dOrg.isZero());
        System.out.printf("Does %s coincide with %s? -- %s%n",point2d1,point2dOrg,point2d1.equals(point2dOrg));
        
        System.out.println("Please enter the coordinates of p31:");
        x = sc.nextDouble();
        y = sc.nextDouble();
        z = sc.nextDouble();
        sc.close();

        Point3D point3d = new Point3D(x, y, z);
        Point3D point3dOrg = new Point3D();
        System.out.printf("The distance from %s to the origin is %s%n",point3d,filter(point3d.getDistance()));
        System.out.printf("Is %s the origin? -- %s%n",point3d,point3d.isZero());
        System.out.printf("Is (0,0,0) the origin? -- %s%n",point3dOrg.isZero());
        System.out.printf("Does %s coincide with %s? -- %s%n",point3d,point3dOrg,point3dOrg.equals(point3d));

    }
}
